package cn.laixueit.crawlerutil;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class CrawlerUtil {

    public static final String BIQUGE_URL = "http://www.xbiquge.la/xiaoshuodaquan/";

    static PoolingHttpClientConnectionManager cm = null;

    static{
        cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(100);
    }

    // 爬取所有小说到数据库
    public static List<Map> getBookName(String url){
        List<Map> list = new ArrayList<>();
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;

        try {
            response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode()==200){
                String content = EntityUtils.toString(response.getEntity(), "utf-8");
                Document document = Jsoup.parse(content);
                Elements elements = document.select("#main ul li a");
                for (Element element : elements) {
                    Map<String,String> map = new LinkedHashMap<>();
                    String href = element.attr("href");
                    String text = element.text();
                    map.put("bookName",text);
                    map.put("linkUrl",href);
                    list.add(map);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    // 根据小说所有章节地址url爬取所有章节
    public static List<Map> getBookChapter(String url){
        List<Map> list = new ArrayList<>();
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;

        try {
            response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode()==200){
                String content = EntityUtils.toString(response.getEntity(), "utf-8");
                Document document = Jsoup.parse(content);
                Elements elements = document.select("#list dd a");
                int i = 1;
                for (Element element : elements) {
                    Map<String,Object> map = new LinkedHashMap<>();
                    String href = element.attr("href");
                    String text = element.text();
                    map.put("chapter",text);
                    map.put("linkUrl",href);
                    map.put("sort",i);
                    i++;
                    list.add(map);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    // 爬取章节内容
    public static String crawlerBookContent(String url){
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response ;
        try {
            response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode()==200){
                String content = EntityUtils.toString(response.getEntity(), "utf-8");
                Document document = Jsoup.parse(content);
                Elements elements = document.select("div#content");
                String text = elements.first().html();
                text = text.replace("<p><a href=\"http://koubei.baidu.com/s/xbiquge.la\" target=\"_blank\">亲,点击进去,给个好评呗,分数越高更新越快,据说给新笔趣阁打满分的最后都找到了漂亮的老婆哦!</a><br>手机站全新改版升级地址：http://m.xbiquge.la，数据和书签与电脑站同步，无广告清新阅读！</p>", "sayBook 无广告清新阅读！");
                return text;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "内容正在获取中，请稍后再来查阅";
    }
}
